﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for ContactPerson.xaml
    /// </summary>
    public partial class ContactPerson : Window
    {
        private string ID;
        Boolean isUbah = false;

        public ContactPerson(string ID_Penyewaan)
        {
            this.ID = ID_Penyewaan;
            InitializeComponent();
            cancelButton.Content = "Clear";
        }
        public ContactPerson(string ID_Penyewaan, string ID_CP)
        {
            this.ID = ID_CP;
            isUbah = true;
            InitializeComponent();
            label1.Content = "Edit Contact Person";
            addButton.Content = "Edit";
            cancelButton.Content = "Cancel";
            this.Title = "TAMS UP :: Form :: Edit Contact Person";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                try
                {
                    homeWindow.Con.Open();

                    SqlDataAdapter da = new SqlDataAdapter("select * from ContactPerson where ID_CP = '" + ID + "';", homeWindow.Con);
                    DataSet ubah = new DataSet();
                    da.Fill(ubah, "delete CP");

                    nama.Text = ubah.Tables["delete CP"].Rows[0].ItemArray.ElementAt(1).ToString();
                    telepon.Text = ubah.Tables["delete CP"].Rows[0].ItemArray.ElementAt(2).ToString();
                    email.Text = ubah.Tables["delete CP"].Rows[0].ItemArray.ElementAt(3).ToString();
                    keterangan.Text = ubah.Tables["delete CP"].Rows[0].ItemArray.ElementAt(4).ToString();

                    homeWindow.Con.Close();
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
                }
            }
            
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                homeWindow.Con.Open();
                SqlDataAdapter da;
                DataSet ds;
                string _Insert;
                SqlCommand _cmd;
                if (errorChecking())
                {
                    if (isUbah)
                    {
                        _Insert = @"update ContactPerson
                                set Nama = '" + nama.Text + "', Nomor_Telepon = '" + telepon.Text + "', email = '" + email.Text + "', keterangan = '" + keterangan.Text + "' " +
                                        "where ID_CP = '" + ID + "';";
                        _cmd = new SqlCommand(_Insert, homeWindow.Con);
                        _cmd.ExecuteNonQuery();
                        homeWindow.Con.Close();
                        topWindow.contactperson.FillData();
                        this.Close();
                    }
                    else
                    {
                        _Insert = @"insert into ContactPerson (ID_CP, Nama, Nomor_Telepon, email, Keterangan)
                                      Values('C" + "','" + nama.Text + "','" + telepon.Text + "','" +
                                                 email.Text + "','" + keterangan.Text + "')";

                        _cmd = new SqlCommand(_Insert, homeWindow.Con);
                        _cmd.ExecuteNonQuery();

                        da = new SqlDataAdapter("select IDENT_CURRENT('ContactPerson')", homeWindow.Con);
                        ds = new DataSet();
                        da.Fill(ds, "Indeks CP");

                        int counts = Int32.Parse(ds.Tables["Indeks CP"].Rows[0].ItemArray.ElementAt(0).ToString());

                        _Insert = @"insert into Menghubungi (ID_CP, ID_Penyewaan)
                                values ('C" + addNol(counts) + counts + "','" + ID + "')";
                        _cmd = new SqlCommand(_Insert, homeWindow.Con);
                        _cmd.ExecuteNonQuery();
                        homeWindow.Con.Close();
                        topWindow.contactperson.FillData();
                        restart();
                    }
                }
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                this.Close();
            }
            else
            {
                nama.Text = "";
                telepon.Text = "";
                email.Text = "";
                keterangan.Text = "";
            }
        }

        private void restart()
        {
            Brush validColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            nama.Text = "";
            nama.Background = validColor;
            telepon.Text = "";
            telepon.Background = validColor;
            email.Text = "";
            email.Background = validColor;
            keterangan.Text = "";
            keterangan.Background = validColor;
        }

        private Boolean errorChecking()
        {
            Boolean valid = true;
            Brush invalidColor = new SolidColorBrush(Color.FromRgb(255, 225, 0));
            Brush ValidColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            if ((nama.Text == "") || (nama.Text.Length > 50))
            {
                nama.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                nama.Background = ValidColor;
            }
            if ((telepon.Text == "")  || (telepon.Text.Length > 20))
            {
                telepon.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                telepon.Background = ValidColor;
            }
            if (!valid)
            {
                MessageBox.Show("Ada kesalahan pengisian informasi tambah contact person", "Invalid Information");
            }
            return valid;
        }

        private string addNol(int x)
        {
            string temp = "";
            string angka = x.ToString();

            if (angka.Count() == 4)
            {
                temp = "0";
            }
            else if (angka.Count() == 3)
            {
                temp = "00";
            }
            else if (angka.Count() == 2)
            {
                temp = "000";
            }
            else if (angka.Count() == 1)
            {
                temp = "0000";
            }
            Console.WriteLine(temp);
            return temp;
        }
    }
}
